<?php

namespace App\Actions;


use App\Model\Closeness;
use App\Model\FollowList;
use App\Model\UserBlacklist;
use App\Utils\MongoDBClient;


class ChatAction
{
    /**
     * Note - 获取聊天屏蔽用户
     * DateTime 2024/8/14 15:50
     * User Admin
     * @param $uid
     * @return array
     */
    public function getMaskUsers($uid)
    {
        $blackList = $this->getBlackList($uid);
        $friend = $this->getClosenessUp($uid, 5);
        $follower = $this->getFollower($uid);
        $chatUser = $this->getChatUser($uid);
        $list = array_merge($blackList, $friend, $follower, $chatUser);
        return array_unique($list);
    }


    /**
     * Note - 获取被我拉黑的用户、获取拉黑我的用户
     * DateTime 2024/8/14 15:55
     * User Admin
     * @param $uid
     * @return array
     */
    public function getBlackList($uid)
    {
        // $sql = "SELECT black_uid uid FROM tb_user_blacklist WHERE uid =$uid AND black_status=0";
        // $rs = $this->model->query($sql);
        // return empty($rs) ? [] : array_column($rs, 'uid');
        // 获取被我拉黑的用户
        $myBlacklist = UserBlacklist::query()->where('uid', $uid)->where('black_status', 0)->get()->pluck('black_uid')->toArray();
        // 获取拉黑我的用户
        $blackoutMe = UserBlacklist::query()->where('black_uid', $uid)->where('black_status', 0)->get()->pluck('uid')->toArray();
        return array_merge($myBlacklist, $blackoutMe);
    }


    /**
     * Note - 获取我关注用户
     * DateTime 2024/8/14 15:54
     * User Admin
     * @param $uid
     * @return array
     */
    public function getFollower($uid)
    {
        // $sql = "SELECT guest_id uid FROM tb_follow_list WHERE main_id=$uid";
        // $rs = $this->model->query($sql);
        // return empty($rs) ? [] : array_column($rs, 'uid');
        return FollowList::query()->where('main_id', $uid)->get()->pluck('guest_id')->toArray();
    }

    /**
     * Note - 获取亲密在5分以上的用户id
     * DateTime 2024/8/14 15:52
     * User Admin
     * @param $uid
     * @return mixed[]
     */
    public function getClosenessUp($uid, $score = 5)
    {
        // $sql = "SELECT anchor_id uid FROM tb_closeness WHERE uid=$uid AND score>=5";
        // $rs = $this->model->query($sql);
        // return empty($rs) ? [] : array_column($rs, 'uid');
        return Closeness::query()->where('uid', $uid)->where('score', '>=', $score)->get()->pluck('anchor_id')->toArray();
    }


    /**
     * Note - 获取聊天用户
     * DateTime 2024/8/14 15:52
     * User Admin
     * @param $uid
     * @return array
     */
    public function getChatUser($uid)
    {
        $mongo = MongoDBClient::getInstance();
        $query = [
            'aggregate' => 'tb_user_chat_v2',
            'cursor' => (object)[],
            'pipeline' => [
                ['$match' => ["receive_uid" => intval($uid), "chat_type" => ['$in' => [3, 4, 5]]]],
                ['$project' => ['uid' => 1]],
                ['$group' => ['_id' => ['uid' => '$uid']]],
            ]
        ];
        $rs = $mongo->query($query);
        $data = [];
        foreach ($rs as $v) {
            $v = $mongo->objectToArray($v);
            $data[] = $v[0]['uid'];
        }
        return $data;
    }
}